package com.takwolf.android.loadmore;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class LoadMoreListView extends ListView implements OnScrollListener {

    private final static int STATE_LOADING = 0;
    private final static int STATE_MANUAL_LOAD_DONE = 1;
    private final static int STATE_AUTO_LOAD_DONE = 2;

    private int state;

    private View footerView;
    private ProgressBar footerProBar;
    private TextView footerTvMessage;

    private boolean canLoadMore = false;
    private boolean isAutoLoadMore = false;

    private OnScrollListener onScrollListener;
    private OnLoadMoreListener onLoadMoreListener; 

    private boolean isOnBottom = false;

    public LoadMoreListView(Context context) {
        super(context);
        init(context);
    }

    public LoadMoreListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public LoadMoreListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context) {
        setCacheColorHint(0x00000000);
        LayoutInflater inflater = LayoutInflater.from(context);
        footerView = inflater.inflate(R.layout.load_more_footer, this, false);
        footerProBar = (ProgressBar) footerView.findViewById(R.id.pull_to_refresh_footer_pro_bar);
        footerTvMessage = (TextView) footerView.findViewById(R.id.pull_to_refresh_footer_tv_message);
        footerView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (canLoadMore && state == STATE_MANUAL_LOAD_DONE) {
                    onLoadMore();
                }
            }

        });
        if (isAutoLoadMore) {
            state = STATE_AUTO_LOAD_DONE;
        } else {
            state = STATE_MANUAL_LOAD_DONE;
        }
        if (canLoadMore) {
            addFooterView(footerView);
            changeFooterViewByState();
        }
        super.setOnScrollListener(this);
    }

    private void changeFooterViewByState() {
        switch (state) {
        case STATE_LOADING:
            footerTvMessage.setText(R.string.load_more_list_view_loading);
            footerProBar.setVisibility(View.VISIBLE);
            break;
        case STATE_MANUAL_LOAD_DONE:
            footerTvMessage.setText(R.string.load_more_list_view_click_to_load);
            footerProBar.setVisibility(View.GONE);
            break;
        case STATE_AUTO_LOAD_DONE:
            footerTvMessage.setText(R.string.load_more_list_view_load_more);
            footerProBar.setVisibility(View.GONE);
            break;
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (canLoadMore && scrollState == SCROLL_STATE_IDLE && isOnBottom && state == STATE_AUTO_LOAD_DONE) {
            onLoadMore();
        }
        if (onScrollListener != null) {
            onScrollListener.onScrollStateChanged(view, scrollState);
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if (firstVisibleItem + visibleItemCount == totalItemCount) {
            isOnBottom = true;
        } else {
            isOnBottom = false;
        }
        if (onScrollListener != null) {
            onScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
        }
    }

    private void onLoadMore() {
        if (onLoadMoreListener != null) {
            state = STATE_LOADING;
            changeFooterViewByState();
            onLoadMoreListener.onLoadMore();
        }
    }

    public void onLoadMoreComplete() {
        if (isAutoLoadMore) {
            state = STATE_AUTO_LOAD_DONE;
        } else {
            state = STATE_MANUAL_LOAD_DONE;
        }
        changeFooterViewByState();
    }

    public void setOnLoadMoreListener(OnLoadMoreListener l) {
        onLoadMoreListener = l;
        setCanLoadMore(true);
    }

    @Override
    public void setOnScrollListener(OnScrollListener l) {
        onScrollListener = l;
    }

    public boolean canLoadMore() {
        return canLoadMore;
    }

    public void setCanLoadMore(boolean canLoadMore) {
        if (this.canLoadMore == canLoadMore) {
            return;
        } else {
            this.canLoadMore = canLoadMore;
            if (canLoadMore) {
                addFooterView(footerView);
                changeFooterViewByState();
            } else {
                removeFooterView(footerView);
            }
        }
    }

    public boolean isAutoLoadMore() {
        return isAutoLoadMore;
    }

    public void setAutoLoadMore(boolean b) {
        isAutoLoadMore = b;
        if (state != STATE_LOADING) {
            if (b) {
                state = STATE_AUTO_LOAD_DONE;
            } else {
                state = STATE_MANUAL_LOAD_DONE;
            }
            changeFooterViewByState();
        }
    }

    public interface OnLoadMoreListener {

        public void onLoadMore();

    }

}
